var user = getCookie("lgc");
if (user) {
    $(".tips").html(`<a href="#">${user}</a> <span></span><div class="exit">退出</div>`).css({ fontFamily: "icomoon" });
    $(".tips").mouseenter(function () {
        $(".exit").css({ display: "block" });
    })
    $(".tips").mouseleave(function () {
        $(".exit").css({ display: "none" });
    })
}
$(".exit").click(function () {
    delCookie("lgc");
    location.reload();
})
$(".LeftHeader").on("click", "li", function () {
    $(this).addClass("mainActive").siblings().removeClass("mainActive");
})
$(".sortLeft").on("click", "li", function () {
    $(this).addClass("sortActive").siblings().removeClass("sortActive");
})

$(".price").mouseenter(function () {
    $(".sorting").css({ display: "block" });
})
$(".price").mouseleave(function () {
    $(".sorting").css({ display: "none" });
})


//价格排序
$(".drop").click(function () {
    var con = $(this).text();
    $(this).parent().prev().children("span").text(con);
    $(this).parent().css({ display: "none" })
    orderCol = $(this).parents(".price").attr("orderCol");
    orderType = "desc";
    leftGoods();
})

$(".elevated").click(function () {
    var con = $(this).text();
    $(this).parent().prev().children("span").text(con);
    $(this).parent().css({ display: "none" })
    orderCol = $(this).parents(".price").attr("orderCol");
    orderType = "asc";
    leftGoods();
})

var keyWords = decodeURIComponent(urlParse()["goodsName"]);
var orderCol = "goodsName";
var orderType = "asc";
var pageIndex = 1;
var showNum = 20;
var maxPage;

$(".searchCon").val(keyWords);

// 左边商品列表渲染
leftGoods();
async function leftGoods() {
    var result = await searchByNameLimit({ keyWords, orderCol, orderType, pageIndex, showNum });
    var { status, msg, currentPage, maxPage, list } = result;
    pageIndex = pageIndex > maxPage ? maxPage : pageIndex;
    if (status) {
        if (list.length > 0) {
            var html = "";
            var obj = list.values();
            for (var val of obj) {
                var { goodsId, goodsName, goodsImg, goodsPrice, sales, store, PointOrigin } = val;
                var price = (goodsPrice * 1).toFixed(2);
                html += `<li>
                    <div class="doubi">
                        <a href="../html/goodsDetail.html?goodsId=${goodsId}" class="showImg">
                            <img src="${goodsImg}" alt="">
                        </a>
                        <div class="same">
                            <a href="javascript:;">找同款</a>
                            <a href="javascript:;">找相似</a>
                        </div>
                    </div>
                    <div class="goodsPrice">
                        <span>￥</span><span class="rmb">${price}</span>
                        <div class="packageMail"></div>
                        <p class="payment">${sales}+人付款</p>
                    </div>
                    <div class="row3">
                        <p><a href="../html/goodsDetail.html?goodsId=${goodsId}">${goodsName}</a></p>
                    </div>
                    <div class="row4">
                        <div class="drsr">
                            <span></span>
                            <span></span>
                            <span></span>
                        </div>
                        <div class="store"><a href="#">${store}</a></div>
                        <div class="address">${PointOrigin}</div>
                    </div>
                    <div class="row5">
                        <div class="feature-icons">
                            <a href="javascript:;"></a>
                        </div>
                        <div class="wangwang">
                            <a href="javascript:;"></a>
                        </div>
                    </div>
                    <div class="icon-btn-report">
                        <a href="javascript:;"></a>
                    </div>
                </li>`
            }
            $(".leftGoods ul").html(html);
            $(".pageNum").html(currentPage);
            $(".totalNum").html(maxPage);
        } else {
            $(".leftGoods").html("暂无此商品,敬请期待");
        }
    }
}

$(".prev").click(function () {
    if (pageIndex <= 1) return false;
    pageIndex--;
    leftGoods();
})
$(".next").click(function () {
    if (pageIndex >= maxPage) return false;
    pageIndex++;
    leftGoods();
})

$(".comprehensive").click(function () {
    orderCol = $(this).attr("orderCol");
    leftGoods();
})
$(".sales").click(function () {
    orderCol = $(this).attr("orderCol");
    leftGoods();
})

//右侧商品渲染
rightGoods()
async function rightGoods() {
    var num = Math.ceil(Math.random() * 100);
    var goodsName = decodeURIComponent(urlParse()["goodsName"]);
    var result = await searchByName({ goodsName, num });
    var { status, msg, list } = result;
    if (status) {
        var html = "";
        var obj = list.values();
        for (var val of obj) {
            var { goodsName, goodsPrice, goodsImg, sales, goodsId } = val;
            var price = (goodsPrice * 1).toFixed(2);
            html += `<li>
                    <a href="../html/goodsDetail.html?goodsId=${goodsId}">
                        <img src="${goodsImg}" alt="">
                        <div class="rightFoot">
                            <div class="one"></div>
                            <span>￥</span>
                            <span class="rightPrice">${price}</span>
                            <div class="rightSales">销量:<i>${sales}</i></div>
                        </div>
                    </a>
                    <div class="like">
                        <div class="like-hd">
                            <a href="javascript:;">喜欢</a>
                            <a href="javascript:;">不喜欢</a>
                        </div>
                        <div class="like-bd">
                            <p>${goodsName}</p>
                        </div>
                    </div>
                </li>`
        }
        $(".rightGoods").html(html);
    }
}



function urlParse() {
    var search = location.search;
    var data = {};
    if (search) {
        var str = search.substr(1);
        var list = str.split("&");
        for (var i = 0; i < list.length; i++) {
            var key = list[i].split("=")[0];
            var val = list[i].split("=")[1];
        }
        data[key] = val;
    }
    return data;
}